package com.zxs.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zxs.domain.dto.PaymentInput;
import com.zxs.domain.dto.PaymentParamsDto;
import com.zxs.service.KuaiqianPaymentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;

@Controller
public class OrderController {


    @Resource
    private KuaiqianPaymentService kuaiqianPaymentService;

    /**
     * 创建订单
     * @param name  商品名称
     * @param num  商品数量
     * @param price  商品价格
     * @return
     */
    @RequestMapping("/createOrder")
    public String createOrder(String name, Integer num, BigDecimal price, Model  model) {

        PaymentInput paymentInput = new PaymentInput();
        //商品ID
        paymentInput.setProductId("SP001");
        //商品描述
        paymentInput.setProductDesc(name);
        //商品名称
        paymentInput.setProductName(name);
        //商品数量
        paymentInput.setProductNum(String.valueOf(num));
        //用户ID
        paymentInput.setPayerIdUserId("DL001");
        //邮箱
        paymentInput.setPayerContact("2889926359@qq.com");
        //订单ID  使用雪花算法生成唯一ID
        long orderId = IdWorker.getId();
        paymentInput.setOrderId(String.valueOf(orderId));
        //订单金额
        BigDecimal orderAmount = price.multiply(BigDecimal.valueOf(num));
        paymentInput.setOrderAmount(orderAmount);

        //调用service，获取快钱接口需要的参数和值
        PaymentParamsDto payment = kuaiqianPaymentService.createPaymentDto(paymentInput);

        System.out.println("payment = " + payment);

        //payment,提交请求给块钱的视图的页面
        model.addAttribute("pay", payment);
        return "payment";
    }

}
